package com.tencet.dao.impl;

import com.tencet.dao.AccountOneDAO;
import com.tencet.entity.AccountOne;
import com.tencet.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: Fame
 * @date: 2022-07-20 20:30
 * @description:
 */
public class AccountOneDAOImpl implements AccountOneDAO {
    private QueryRunner runner = new QueryRunner(JDBCUtils.getDataSource());
    @Override
    public List<AccountOne> getAccountOnrByIdTwo(String idTwo) {
        String sql = "select * from bank_account1 where account_id2 = ?";
        try {
            return runner.query(sql,new BeanListHandler<AccountOne>(AccountOne.class),idTwo);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer getBalanceByIdOne(String idOne) {
        String sql = "select balance from bank_account1 where account_id1 = ?";
        try {
            return (Integer) runner.query(sql, new ScalarHandler(), idOne);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return  null;
    }

    /**
     * 将绑卡关系存入数据库表bank_account1
     * @param id:uuid
     * @param cardNum:一类卡号
     * @param card2id
     * @return
     */
    @Override
    public int insert(String id, String cardNum, String card2id) {
        String sql = "insert into bank_account1 values(?,1,?,0,?)";
        try {
            return runner.update(sql,id,cardNum,card2id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return  -1;
    }
}
